<!--预审结果  -->
<template>
  <div class="declare-confirm-dialogShow">
    <el-dialog
      :title="title"
      :visible.sync="declareAndConfirmDialogShow"
      width="628px"
      :before-close="handleClose"
      custom-class="dialog-box"
    >
      <el-form
        :model="queryParams"
        :rules="rules"
        ref="queryParams"
        label-width="80px"
      >
        <el-form-item label="请选择">
          <el-select
            v-model="selectWay"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in selectWayOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="专家建议"
          prop="approvalResult"
          v-if="selectWay === 0"
        >
          <el-radio-group v-model="queryParams.approvalResult">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="5">退回重申</el-radio>
            <!-- <el-radio :label="3">退回重申</el-radio> -->
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="批复附件"
          prop="approvalDatabase.fileName"
          v-if="selectWay === 0"
        >
          <el-upload
            ref="upload"
            class="upload-demo"
            :action="upload.url"
            accept=".png, .jpg, .pdf, .doc, .docx, .zip, .rar, .7z"
            :headers="upload.headers"
            :limit="1"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
          >
            <el-input
              v-model="queryParams.approvalDatabase.fileName"
              placeholder="请选择文件"
              style="width:100%"
            />
            <div class="img-box">
              <img
                src="@/assets/images/tripartite/icon_files_upload.png"
                alt=""
              />
            </div>
          </el-upload>
        </el-form-item>
        <div
          class="select-template"
          @click="handLastApprovalResult"
          v-if="selectWay === 0"
        >
          <span class="el-dropdown-link">
            引用上次预审意见<i
              class="icon-arrow-down el-icon-folder-remove"
            ></i>
          </span>
        </div>
        <el-form-item
          label="预审意见"
          prop="approvalOpinion"
          v-if="selectWay === 0"
        >
          <el-input
            type="textarea"
            v-model="queryParams.approvalOpinion"
            show-word-limit
            maxlength="1000"
          ></el-input>
        </el-form-item>
        <el-descriptions
          style="margin: 20px 0"
          :column="1"
          border
          v-if="selectWay === 1"
        >
          <el-descriptions-item>
            <template slot="label"> 任务名称 </template>
            {{ cancenApplyData.taskName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"> 任务节点 </template>
            {{ cancenApplyData.node }}
          </el-descriptions-item>
        </el-descriptions>
        <el-form-item
          label="申请理由"
          prop="applyReason"
          v-if="selectWay === 1"
        >
          <el-input
            type="textarea"
            v-model="queryParams.applyReason"
            maxlength="200"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

import {
  getApprovalAdd,
  getUpdata,
  getDetailByTask,
  getExtReceiveInfo,
  getApplyCancel,
} from "@/api/manage/manage";
import { getLastApprovalResult } from "@/api/expertEvaluation/expertEvaluation";
export default {
  name: "declareDialog",
  props: {
    declareAndConfirmDialogShow: {
      type: Boolean,
      default: false,
    },
    type: {
      type: String,
      default: "",
    },
    proId: {
      type: String,
      default: "",
    },
    expertId: {
      type: String,
      default: "",
    },
    reviewTaskId: {
      type: String,
      default: "",
    },
  },
  inject: ["reload"],
  data() {
    return {
      title: "预审结果",
      rules: {
        approvalResult: [
          { required: true, message: "请选择预审结果", trigger: "change" },
        ],
        "approvalDatabase.fileName": [
          { required: true, message: "请选择文件", trigger: "change" },
        ],
        // approvalOpinion: [
        //   { required: true, message: "请输入", trigger: "blur" },
        // ],
      },
      queryParams: {
        approvalDatabase: { fileName: "", fileUrl: "" },
        approvalResult: undefined,
        approvalOpinion: undefined,
        applyReason: "", //申请理由
      },
      // 上传文件参数
      upload: {
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/file/upload",
        // 上传文件名称
        fileName: "请选择文件",
      },
      // 选择模板数据字典
      selectDemoList: [
        {
          dictValue: "未对接口,上次意见暂未获取",
          dictLabel: "未对接口,上次意见暂未获取",
        },
      ],
      selectWay: 0,
      // 选择方式数据字典
      selectWayOptions: [
        { dictValue: 0, dictLabel: "审批" },
        { dictValue: 1, dictLabel: "申请取消" },
      ],
      cancenApplyData: {},
    };
  },
  watch: {
    selectWay(val) {
      this.$refs.queryParams.resetFields();
      console.log(val, "val");
      if (val === 0) {
        this.title = "审批";
      } else {
        this.title = "申请取消";
        getExtReceiveInfo(this.reviewTaskId, this.expertId).then((res) => {
          if (res.code == 200) {
            this.cancenApplyData = res.data;
          }
        });
      }
    },
    declareAndConfirmDialogShow(val) {},
  },
  methods: {
    /** 取消按钮 */
    handleClose() {
      this.selectWay = 0;
      this.$refs.queryParams.resetFields();
      this.$emit("changeShowDialog", "false"); //调用父组件的自定义事件，并传值
    },
    // 选择模板内容
    handleCommand(command) {
      this.queryParams.approvalOpinion = command;
    },
    // 引用上次预审意见
    handLastApprovalResult() {
      getLastApprovalResult(this.expertId).then((res) => {
        if (res.code == 200 && res.msg) {
          this.queryParams.approvalOpinion = res.msg;
        } else {
          this.$message.error("无法获取到上次预审意见,可自行填写意见内容");
        }
      });
    },
    /** 确定按钮 */
    submitForm() {
      this.$refs.queryParams.validate((valid) => {
        if (valid) {
          if (this.selectWay === 0) {
            this.queryParams.projectId = this.proId;
            getApprovalAdd(this.queryParams).then((res) => {
              if (res.code == 200) {
                this.$refs.queryParams.resetFields();
                this.$emit("changeShowDialog", "false");
                this.$message.success("审批成功");
                this.reload();
              }
            });
          } else {
            this.$confirm("是否确认申请 ？", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
              customClass: "del_box",
            })
              .then(() => {
                this.queryParams.reviewTaskId = this.reviewTaskId;
                getApplyCancel(this.queryParams).then((res) => {
                  if (res.code == 200) {
                    this.$refs.queryParams.resetFields();
                    this.$emit("changeShowDialog", "false");
                    this.$message.success("申请成功");
                    this.reload();
                  }
                });
              })
              .catch(() => {});
          }
        }
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      console.log("文件上传中", file);
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      console.log("文件上传成功", response, file);
      this.$refs.upload.clearFiles();
      if (response.code != 500 && response.data) {
        this.upload.fileName = file.name;
        this.queryParams.approvalDatabase.fileName = file.name;
        this.queryParams.approvalDatabase.fileUrl = response.data.fileUrl;
        this.$forceUpdate();
      } else {
        this.$message.error(response.msg);
      }
    },
  },
};
</script>

<style lang="scss">
.declare-confirm-dialogShow .dialog-box .el-form-item .el-input__inner {
  width: 100%;
}
.declare-confirm-dialogShow .dialog-box .el-form-item .el-textarea__inner {
  width: 100%;
}
.declare-confirm-dialogShow {
  // 下载模板样式修改
  .select-template {
    .el-dropdown-link {
      color: #3171ee;
    }
    .icon-arrow-down {
      margin-left: 10px;
      color: #3171ee;
    }
    // width: 100%;
    display: flex;
    align-items: center;
    justify-content: right;
    font-size: 14px;
    font-family: PingFang SC, PingFang SC-Regular;
    font-weight: 400;
    text-align: LEFT;
    color: #3171ee;
    margin-left: 30%;
    margin-bottom: 5px;
    cursor: pointer;
    img {
      margin-left: 10px;
    }
  }
  // 文件上传
  .upload-demo {
    .el-upload-list {
      display: none;
    }
    .el-upload {
      display: flex;
    }
    .img-box {
      height: 37px;
      width: 37px;
      line-height: 36px;
      background: #0d72ff;
      text-align: center;
      border-radius: 0px 4px 4px 0px;
      position: absolute;
      right: -1%;
      top: 1px;
      img {
        width: 16px;
        height: 16px;
      }
    }
  }
}
</style>
